package com.lianda.java8.functional.base.benchmark;

import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

//lambda调用测试
public class LambdaTest {
    public static void main(String[] args) throws Throwable {
        GetterFunction getterFunction;
        final MethodHandles.Lookup lookup = MethodHandles.lookup();
        MethodType methodType = MethodType.methodType(String.class, Person.class);
        final CallSite site = LambdaMetafactory.metafactory(lookup,
                "invoke",
                MethodType.methodType(GetterFunction.class),
                methodType,
                lookup.findVirtual(Person.class, "getName", MethodType.methodType(String.class)),
                methodType);

        long start = System.currentTimeMillis();
        getterFunction = (GetterFunction) site.getTarget().invokeExact();
        System.out.println(getterFunction.invoke(new Person("Ann")));
        long end = System.currentTimeMillis();


        System.out.println("反射函数式方法调用执行了， 耗时" + (end -start) + "ms");
    }
}
